'use strict';
const Service = require('../core/base_service.js');
class UserService extends Service {

  /** 
   * @description 查询角色
   * @param {*} roleid
   * @returns
   * @memberof UserService
   */
  async findRole(roleid) {
    const result = await this.app.mysql.select('role', {
      where: {
        id: roleid,
      }, // WHERE 条件
      columns: ['RoleName'], // 要查询的表字段
    });
    console.log("sevice++++++++")
    console.log(result)
    return result;
  }
  /**
   *
   * @description 查询用户
   * @param {*} uid
   * @returns
   * @memberof UserService
   */

  async find(uid) {
    // 假如 我们拿到用户 id 从数据库获取用户详细信息
    const result = await this.app.mysql.query('select id,name,RoleName,user.roleid from user join role where id = ?',[uid]);

    console.log("find result_____")
    console.log(result)
    const arr_result = Object.keys(result);
    if (arr_result.length === 0) {
      return {
        code: 404,
        msg: '没有此用户',

      };
    }
    return {
      result,
    };
  }

  /**
   * @param {*} uid
   * @return
   * @memberof UserService
   */

  async changePwd(uid) {
    const user_f = await this.app.mysql.select('user', {
      where: {
        id: uid,
      }, // WHERE 条件
      columns: ['id', 'name'], // 要查询的表字段
    });
    return {
      user_f,
    };
  }


  /**
   *
   * @description 创建用户
   * @param {*} username
   * @param {*} pwd
   * @param {*} roleid
   * @memberof UserService
   */

  async createUser(username, pwd, roleid) {
    const result = this.app.mysql.insert('user', {
      name: username,
      pwd,
      RegisterTIme: this.app.mysql.literals.now,
      roleid,
    });

    const result_m = this.check_result_mysql(result);

    return result_m;
  }

  /**
   *ƒ
   * @description 修改用户信息
   * @param {*} userid
   * @param {*} editD
   * @returns
   * @memberof UserService
   */

  async editUser(username, user, editD) {
    let temp = user

    //是不是第一次登陆
    if (temp.firstLoginTime) {
      editD.firstLoginTime = this.app.mysql.literals.now
    }

    //更新登陆时间
    temp.loginTime = this.app.mysql.literals.now
    // editD.FirstLoginTime = this.app.mysql.literals.now;

    // assign("target","resourse")
    let row = Object.assign(editD, temp);

    const result = await this.app.mysql.update('user', row); // 更新 posts 表中的记录

    return this.check_result_mysql(result);
  }

  async findUsername(username, pwd) {

    const result = await this.app.mysql.select('user', {
      where: {
        userName: username,
        pwd,
      }, // WHERE 条件
      columns: ['id', 'userName', 'firstLoginTime'], // 要查询的表字段
      limit: 10, // 返回数据量
    });

    this.ctx.logger.info('arr_result+++++++++', JSON.stringify(result));
    typeof result[0] !== 'object' ? result[0] = {} : result[0];

    let arr_result = '';
    if (result[0]) {
      arr_result = Object.assign(result[0], {
        code: 2000
      });
    }

    this.ctx.logger.info('arr_result}}}}}}}}', arr_result);
    return arr_result;
  }
}

module.exports = UserService;